<?php
if( !defined('BASEPATH') ) exit ( 'No direct script access allowed' );

class Article extends admin_controller
{
	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$sql = $this->db->order_by('id', 'desc')->get_where('article', array('type' => 'article'))->result_array();

		$this->template->title('Quản lý bài viết')
			->set('body', $sql)
			->set('link_add', URL_ADMIN.'article/add')
			->set('link_edit', URL_ADMIN.'article/edit/')
			->set('link_del', URL_ADMIN.'article/delete/')
            ->set('m_active', '#m_content')
			->build('admin/article/index');
	}

	public function add()
	{

		if($_POST)
		{
			//print_r($_POST);

			$this->form_validation->set_rules('title', 'Tiêu đề', 'required');
			$this->form_validation->set_rules('intro', 'Mô tả ngắn', 'required');

			if($this->form_validation->run() == TRUE)
			{

				if(!empty($_FILES['pic']['name']))
                {
                    $config['upload_path'] = './data_uploads/image/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['encrypt_name'] = TRUE;

                    $this->load->library('upload', $config);

                    if ( ! $this->upload->do_upload('pic'))
                    {
                        echo $this->upload->display_errors();
                        $image = '';
                    }
                    else
                    {
                        $up = $this->upload->data();
                        $image = 'data_uploads/image/' . $up['file_name'];

						$imageWidth = $up['image_width'];
                        if($imageWidth > 150)
                        {
                            $this->load->library('image_lib');
                            $config['image_library'] = 'GD2';
                            $config['source_image'] = $up['full_path'];
                            $config['quality'] = 75;
                            $config['maintain_ratio'] = TRUE;
                            $config['master_dim'] = 'auto';
                            $config['width'] = 150;
                            $config['height'] = 100;

                            $this->image_lib->initialize($config);
                            $this->image_lib->resize();
                        }

                    }
                } else {
                    $image = '';
                }

				$arr = array(
					'title'			=> $this->input->post('title'),
					'intro'			=> $this->input->post('intro'),
					'content'		=> $this->input->post('description'),
					'status'		=> (int)$this->input->post('status'),
					'order'			=> (int)$this->input->post('order'),
					'hot'			=> (int)$this->input->post('hot'),
					'image'			=> $image,
					'cat_id'		=> $this->input->post('cat_id'),
					'date_added'	=> time(),
					'type'         => 'article'

				);

				if( $this->db->insert('article', $arr) )
				{
					$this->message('info', 'Added successfuly');
					redirect(URL_ADMIN.'article');
				} else {
					$this->message('info', 'Added faild');
					redirect(URL_ADMIN.'article');
				}
			}

		}

		$this->db->order_by('order', 'asc');
		$cates = $this->db->get_where('category', array('type' => 'article'))->result_array();

		$this->template->title('Thêm bài viết')
			->set('link_action', URL_ADMIN.'article/add')
			->set('link_cancel', URL_ADMIN.'article')
			->set('cates', $cates)
            ->set('m_active', '#m_content')
			->build('admin/article/add');

	}

	public function edit()
	{
		$id = $this->uri->segment(4);

		if($_POST)
		{

			$this->form_validation->set_rules('title', 'Tiêu đề', 'required');
			$this->form_validation->set_rules('intro', 'Mô tả ngắn', 'required');

			$oldPic = $this->input->post('oldImage');

			if($this->form_validation->run() == TRUE)
			{

				if(!empty($_FILES['pic']['name']))
                {
                    $config['upload_path'] = './data_uploads/image/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['encrypt_name'] = TRUE;

                    $this->load->library('upload', $config);

                    if ( ! $this->upload->do_upload('pic'))
                    {
                        //echo $this->upload->display_errors();
                        $image = $oldPic;
                    }
                    else
                    {
                        $up = $this->upload->data();
                        $image = 'data_uploads/image/' . $up['file_name'];

						if($oldPic !='') @unlink($oldPic);

						$imageWidth = $up['image_width'];
                        if($imageWidth > 150)
                        {
                            $this->load->library('image_lib');
                            $config['image_library'] = 'GD2';
                            $config['source_image'] = $up['full_path'];
                            $config['quality'] = 75;
                            $config['maintain_ratio'] = TRUE;
                            $config['master_dim'] = 'auto';
                            $config['width'] = 150;
                            $config['height'] = 100;

                            $this->image_lib->initialize($config);
                            $this->image_lib->resize();
                        }

                    }
                } else {
                    $image = $oldPic;
                }

				$arr = array(
					'title'			=> $this->input->post('title'),
					'intro'			=> $this->input->post('intro'),
					'content'		=> $this->input->post('description'),
					'status'		=> (int)$this->input->post('status'),
					'order'			=> (int)$this->input->post('order'),
					'hot'			=> (int)$this->input->post('hot'),
					'image'			=> $image,
					'cat_id'		=> $this->input->post('cat_id'),
					//'date_added'	=> time()

				);

				if( $this->db->update('article', $arr, array( 'id' => $id )) )
				{
					$this->message('info', 'Updated successfuly');
					redirect(URL_ADMIN.'article');
				} else {
					$this->message('info', 'Updated faild');
					redirect(URL_ADMIN.'article');
				}
			}

		}

		$items = $this->db->get_where('article', array('id' => $id))->row_array();
		$this->db->order_by('order', 'asc');
		$cates = $this->db->get_where('category', array('type' => 'article'))->result_array();

		$this->template->title('Chỉnh sửa bài viết')
			->set('link_action', URL_ADMIN.'article/edit/'. $id)
			->set('link_cancel', URL_ADMIN.'article')
			->set('items', $items)
			->set('cates', $cates)
            ->set('m_active', '#m_content')
			->build('admin/article/edit');
	}

	public function delete()
	{
		$id = $this->uri->segment(4);

		$img = $this->db->select('image')->get_where('article', array('id' => $id))->row_array();

		if($img['image'] !=='') @unlink($img['image']);

		if ( $this->db->delete('article', array( 'id' => $id )) )
		{
			$this->message('info', 'Deleted successfuly');
			redirect(URL_ADMIN.'article');
		} else {
			$this->message('info', 'Deleted successfuly');
			redirect(URL_ADMIN.'article');
		}

	}
}

/* End file */
/* Local: ./application/controller/admin/page.php */